package cn.mrlong.aavdemo.surfaceview;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.mrlong.aavdemo.R;

/**
 * SurfaceView显示camera
 * 编码获取到的数据
 */
public class SurfaceCameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private Camera camera;
    private SurfaceView surfaceView;
    private H264Encoder h264Encoder;
    private int width = 640;
    private int height = 480;
    private int framerate = 30;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surface_view2);
        surfaceView = findViewById(R.id.surface);
        surfaceView.getHolder().addCallback(this);
        //打开摄像头 旋转90度
        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
        camera.setDisplayOrientation(90);
        //Camera Preview Callback的YUV常用格式有两种：一个是NV21，一个是YV12。Android一般默认使用YCbCr_420_SP的格式（NV21）。
        Camera.Parameters parameters = camera.getParameters();
        //不设置这个可能是花的
        parameters.setPreviewSize(width, height);
        parameters.setPreviewFormat(ImageFormat.NV21);
        camera.setParameters(parameters);
        camera.setPreviewCallback(new Camera.PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                //通过setPreviewCallback方法监听预览的回调：
                //一个音视频文件是由音频和视频组成的，我们可以通过MediaExtractor、MediaMuxer把音频或视频给
                //单独抽取出来，抽取出来的音频和视频能单独播放；
                H264Encoder.putYUVData(data);//加入队列中
            }
        });
        h264Encoder = new H264Encoder();
        btn = findViewById(R.id.start);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("开始录制".equals(btn.getText().toString())) {
                    h264Encoder.start(width, height, framerate);
                    btn.setText("停止录制");
                } else if ("停止录制".equals(btn.getText().toString())) {
                    h264Encoder.stopThread();
                    btn.setText("开始录制");
                }
            }
        });
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        try {
            camera.setPreviewDisplay(holder);
            camera.startPreview();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        try {
            camera.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
